#include "stdafx.h"
#include "AudioSystem.h"


System_Audio :: System_Audio()
{
	CurrentBGM		= "";
	m_fBGMVolume	= 80.0f;
	m_fSFXVolume	= 100.0f;
	m_fVoiceVolume	= 100.0f;
}

System_Audio :: ~System_Audio()
{
	//StopAll();
}

bool System_Audio :: Initialize()
{
	m_pAudioDevice = audiere :: OpenDevice();
	return (m_pAudioDevice)? true : false;
}

void System_Audio :: Update(const float & Interval)
{
	if(TrackShifter.size() > 1)
	{
		if(m_fVolume > 0)
			m_fVolume -= m_fDeltaV * Interval;
		else
		{
			TrackShifter.pop_front();
			m_pBGMStream->stop();
			PlayBGM(TrackShifter.front());
		}
	}
	m_pAudioDevice->update();
}

void System_Audio :: StopAll()
{
	m_pBGMStream->stop();
	std :: map <std :: string, audiere :: OutputStreamPtr> :: iterator it = SFXStreams.begin();
	for(; it != SFXStreams.end(); it++)
		(*it).second->stop();
}

void System_Audio :: PlayBGM(const std::string & File)
{
	std :: string Filename = System_Utility :: LowerCase(File);
	if(Filename == CurrentBGM)
	{
		WriteCommandLine("Identical BGM");
		return;
	}
	if(m_pBGMStream)
		if(m_pBGMStream->isPlaying())
			m_pBGMStream->stop();

	DWORD size = 0;
	void * Buffer	= m_pHGESystem->Resource_Load(Filename.c_str(), &size);
	if(!Buffer) 
	{
		WriteCommandLine("Error loading BGM");
		return;
	}
	audiere :: FilePtr			pBGMFile		= audiere :: CreateMemoryFile(Buffer, size);
	audiere :: SampleSourcePtr	pBGMSourcePtr	= audiere :: OpenSampleSource(pBGMFile);

	m_pBGMStream = audiere :: OpenSound(m_pAudioDevice, pBGMSourcePtr, true);
	m_pBGMStream->setRepeat(true);
	m_pBGMStream->setVolume(m_fBGMVolume);
	m_pBGMStream->play();
	
	m_pHGESystem->Resource_Free(Buffer);
	pBGMFile = NULL;
	pBGMSourcePtr = NULL;
	Buffer = NULL;
	CurrentBGM = Filename;
}

void System_Audio :: SetNextBGM(const std :: string & FileName,  const float & FadeSpd)
{
	m_fDeltaV = FadeSpd;
	TrackShifter.push_back(FileName);
	m_fVolume = m_pBGMStream->getVolume();
}

audiere :: OutputStreamPtr	System_Audio :: GetSFX(const std :: string & File)
{
	std :: string FileName = System_Utility :: LowerCase(FileName);
	std :: map <std :: string, audiere :: OutputStreamPtr> :: iterator it = SFXStreams.find(FileName);
	if(it != SFXStreams.end())
		return (*it).second;

	DWORD size = 0;
	void * Buffer	= m_pHGESystem->Resource_Load(FileName.c_str(), &size);
	if(!Buffer) 
	{
		WriteCommandLine("Error Loading SFX");
		return NULL;
	}
	audiere :: FilePtr			pFile		= audiere :: CreateMemoryFile(Buffer, size);
	audiere :: SampleSourcePtr	pSourcePtr	= audiere :: OpenSampleSource(pFile);
	audiere :: OutputStreamPtr	SFXPtr = audiere :: OpenSound(m_pAudioDevice, pSourcePtr, false);
	
	SFXPtr->setVolume(m_fSFXVolume);
	SFXPtr->setRepeat(false);
	m_pHGESystem->Resource_Free(Buffer);
	pFile = NULL;
	pSourcePtr = NULL;
	Buffer = NULL;

	return SFXStreams.insert(std::make_pair(FileName, SFXPtr)).first->second;
}

bool System_Audio :: RemoveSFX(const std :: string & File)
{
	std :: string FileName = System_Utility :: LowerCase(FileName);
	std :: map <std :: string, audiere :: OutputStreamPtr> :: iterator it = SFXStreams.find(FileName);
	if(it != SFXStreams.end())
	{
		SFXStreams.erase(it);
		return true;
	}
	return false;
}